package com.lin.bank.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.bank.config.exception.BusinessException;
import com.lin.bank.config.reponse.Result;
import com.lin.bank.config.reponse.ResultCode;
import com.lin.bank.entity.TransactionRecord;
import com.lin.bank.feign.RecordsFeign;
import com.lin.bank.mapper.TransactionRecordMapper;
import com.lin.bank.service.TransactionRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "账单管理")
@RestController
public class TransactionRecordController {

    @Resource
    private TransactionRecordMapper mapper;

    @Autowired
    private TransactionRecordService service;

    @ApiOperation(value = "获取所有转账信息")
    @GetMapping("/getAll")
    public Result getAll() {
        List<TransactionRecord> list = mapper.selectList(null);
        return Result.ok().data("records", list);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd mm:HH:ss");
        //注册自定义的编辑器
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }

    @GetMapping("/selectPage")
    @ApiOperation(value = "分页查询用户转账信息列表")
    public Result selectPage(@RequestParam(name = "cardNo", required = true) String cardNo,
                             @RequestParam(name = "transactionDate1", required = true) String transactionDate1,
                             @RequestParam(name = "transactionDate2", required = true) String transactionDate2,
                             @RequestParam(name = "current", required = false, defaultValue = "1") Integer current,
                             @RequestParam(name = "size", required = false, defaultValue = "4") Integer size) {
        IPage<TransactionRecord> recordIPage = service.selectPage(cardNo, transactionDate1, transactionDate2, current, size);
        return Result.ok().data("page", recordIPage);
    }

    @ApiOperation(value = "发送消息到消息队列")
    @GetMapping("/sendMsg")
    public String sendMsg(@RequestParam(name = "cardNo") String cardNo,
                          @RequestParam(name = "cardNo2")String cardNo2,
                          @RequestParam(name = "amount")Double amount,
                          @RequestParam(name = "balance1")Double balance1,
                          @RequestParam(name = "balance2")Double balance2) {
        String s = service.sendMsg(cardNo, cardNo2, amount,balance1,balance2);
        return s;
    }



}
